/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.softao.ming.gwt.user.client.layout.AbstractAbsoluteLayout;

/**
 * Provides grid style editing with fixed width. It inherits from
 * {@link AbstractAbsoluteLayout}, which provides the layout feature.
 */
public class AbsoluteLayoutForm extends AbstractAbsoluteLayout {
	private int mLabelSinkage = 4;

	/**
	 * Initializes a new instance of <code>GridEditForm</code>.
	 * 
	 * @param pattern
	 */
	public AbsoluteLayoutForm(String pattern) {
		super(pattern);

		this.getLayoutWidget().setStyleName("ming-absoluteLayoutForm");
	}

	@Override
	protected AbsolutePanel createPositionPanel() {
		AbsolutePanel panel = new AbsolutePanel();
		panel.setStyleName("absoluteLayoutFormPosition");
		return panel;
	}

	@Override
	protected void putWidget(Widget widget, AbsolutePanel layoutWidget,
			PositionDefinition positionDef) {
		int left = positionDef.getLeft();
		int top = positionDef.getTop();

		if (widget instanceof Label) {
			top += this.mLabelSinkage;
		}

		layoutWidget.add(widget, left, top);

		if (positionDef.getWidth() > 0 && positionDef.getHeight() > 0) {
			widget.setPixelSize(positionDef.getWidth(), 
					positionDef.getHeight());
		} else if (positionDef.getWidth() > 0) {
			widget.setWidth(positionDef.getWidth() + "px");
		} else if (positionDef.getHeight() > 0) {
			widget.setHeight(positionDef.getHeight() + "px");
		}
		this.setChildStyle(widget, positionDef);
	}

	/**
	 * Sets the child style.
	 * <p>
	 * As a edit form, a predefined list of the child widgets will be applied
	 * with the default primary style sheet, and user can dependency style name
	 * as necessary.
	 */
	@Override
	protected void setChildStyle(Widget widget, PositionDefinition positionDef) {
		// if (widget instanceof Label) {
		// widget.setStylePrimaryName("absoluteLayoutFormLabel");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof TextBox) {
		// widget.setStylePrimaryName("absoluteLayoutFormTextBox");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof TextArea) {
		// widget.setStylePrimaryName("absoluteLayoutFormTextArea");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof ListBox) {
		// widget.setStylePrimaryName("absoluteLayoutFormListBox");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof CheckBox) {
		// widget.setStylePrimaryName("absoluteLayoutFormCheckBox");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof RadioButton) {
		// widget.setStylePrimaryName("absoluteLayoutFormRadioButton");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof Button) {
		// widget.setStylePrimaryName("absoluteLayoutFormButton");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof Hyperlink) {
		// widget.setStylePrimaryName("absoluteLayoutFormHyperlink");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else if (widget instanceof PasswordTextBox) {
		// widget.setStylePrimaryName("absoluteLayoutFormPasswordTextBox");
		// if (positionDef.getStyle() != null
		// && positionDef.getStyle().length() > 0) {
		// widget.addStyleDependentName(positionDef.getStyle());
		// }
		// } else {
		// super.setChildStyle(widget, positionDef);
		// }
	}

	/**
	 * @param value
	 *            the label sinkage to set
	 */
	public void setLabelSinkage(int value) {
		mLabelSinkage = value;
	}

	/**
	 * @return the label sinkage
	 */
	public int getLabelSinkage() {
		return mLabelSinkage;
	}

}
